package com.api.customization.huangwm.localStorage;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.core.Context;
import javax.ws.rs.core.MediaType;

import org.apache.axis.components.logger.LogFactory;
import org.apache.commons.logging.Log;

import weaver.conn.RecordSet;
import weaver.hrm.User;
import weaver.hrm.company.DepartmentComInfo;
import weaver.hrm.company.SubCompanyComInfo;
import weaver.hrm.job.JobTitlesComInfo;
import b.a.a.ac.n;
import cfca.sadk.org.bouncycastle.asn1.dvcs.Data;

import com.engine.common.util.ParamUtil;

/**
 * @author Huangwm
 * @package com.api.customization.huangwm.localStorage
 * @description
 */
public class GetDepartmentInfoApi {

    private Log log = new LogFactory().getLog("custom_huangwm");

    /**
     * 根据部门id获取信息,不传id则获取所有部门信息
     *
     * @param request
     * @param response
     * @return
     */
    @GET
    @Path("/getDepartmentInfo")
    @Produces(MediaType.APPLICATION_JSON)
    public Map<String, Object> get(@Context HttpServletRequest request, @Context HttpServletResponse response) {
        Map<String, Object> apidatas = new HashMap<String, Object>();
        User user = null;
        try {
            HttpSession session = request.getSession();
            user = (User) session.getAttribute("weaver_user@bean");
            Map<String, Object> requestParams = ParamUtil.request2Map(request);
            log.info("/customization/localStorage/department/getDepartmentInfo");
            log.info(requestParams);
            RecordSet rs = new RecordSet();
            String sql = "select id,departmentname,departmentmark,subcompanyid1,supdepid,departmentcode from HrmDepartment ";
            String departmentid = "";
            List<Map<String, String>> result = new ArrayList<Map<String, String>>();
            List<String> ids = new ArrayList<>();
            if (requestParams.containsKey("departmentid")) {
                departmentid = requestParams.get(departmentid).toString();
                sql += " where id=" + departmentid;
                log.info(sql);
                rs.execute(sql);
                while (rs.next()) {
                    Map<String, String> data = new HashMap<>();
                    String id = rs.getString("id");
                    String departmentname = rs.getString("departmentname");
                    String departmentmark = rs.getString("departmentmark");
                    String subcompanyid1 = rs.getString("subcompanyid1");
                    String supdepid = rs.getString("supdepid");
                    String departmentcode = rs.getString("departmentcode");
                    data.put("id", id);
                    data.put("departmentname", departmentname);
                    data.put("departmentmark", departmentmark);
                    data.put("subcompanyid1", subcompanyid1);
                    data.put("supdepid", supdepid);
                    data.put("departmentcode", departmentcode);
                    ids.add(id);
                    result.add(data);
                }
            } else {
                sql += " where canceled<>1 or canceled is null";
                rs.execute(sql);
                log.info(sql);
                while (rs.next()) {
                    Map<String, String> data = new HashMap<>();
                    String id = rs.getString("id");
                    String departmentname = rs.getString("departmentname");
                    String departmentmark = rs.getString("departmentmark");
                    String subcompanyid1 = rs.getString("subcompanyid1");
                    String supdepid = rs.getString("supdepid");
                    String departmentcode = rs.getString("departmentcode");
                    data.put("id", id);
                    data.put("departmentname", departmentname);
                    data.put("departmentmark", departmentmark);
                    data.put("subcompanyid1", subcompanyid1);
                    data.put("supdepid", supdepid);
                    data.put("departmentcode", departmentcode);
                    ids.add(id);
                    result.add(data);
                }
            }

            apidatas.put("apiStatus", true);
            apidatas.put("errorCode", "1");
            apidatas.put("msg", "获取成功");
            apidatas.put("currentuser", user.getUID());
            apidatas.put("result", result);
            apidatas.put("ids", ids);
        } catch (Exception e) {
            e.printStackTrace();
            apidatas.put("apiStatus", false);
            apidatas.put("errorCode", "-1");
            apidatas.put("msg", "获取异常：" + e.getMessage());
            apidatas.put("currentuser", user.getUID() + "");
            apidatas.put("result", null);
        }
        return apidatas;
    }
}
